package com.fs.cv.service.impl;

import com.fs.cv.dao.AbsenceDao;
import com.fs.cv.pojo.pojodao.Absence;
import com.fs.cv.pojo.pojodao.User;
import com.fs.cv.pojo.pojoview.AbsenceCourse;
import com.fs.cv.service.AbsenceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 * @description:
 * @author: 侯俊
 * @createDate: 2020/9/28
 * @version: 1.0
 */
@Service
public class AbsenceServiceImpl implements AbsenceService {
    @Autowired
    private AbsenceDao absenceDao;
    /**
         * 方法描述
         * @return 获取考勤表的所有信息，分页查询
         * @author houjun
         * @date 2020/9/28
         */


    @Override
    public PageInfo<AbsenceCourse> getAbsenceByPage(Integer currentPage, Integer pageSize) {
        //获取当前的登录对象
        Session session = SecurityUtils.getSubject().getSession();
        User user = (User) session.getAttribute("user");
        int userId=user.getUserId();
        //拦截mybatis后续的查询
        PageHelper.startPage(currentPage,pageSize);
        List<AbsenceCourse> absences = absenceDao.queryAll(userId);
        //将查询出来的页面内部计算
        PageInfo<AbsenceCourse> pageInfo = new PageInfo<>(absences);
        return pageInfo;
    }

    @Override
    public Integer getBySname(String studentName) {
        return absenceDao.queryBySname(studentName);
    }

    @Override
    public Integer getByCname(String courseName) {
        return absenceDao.queryByCname(courseName);
    }

    //缺勤表增加
    @Override
    @Transactional//注明当前的事务性
    public int addOne(Absence absence) {
        Integer num = absenceDao.insertOne(absence);
        System.out.println("缺勤表增加-----------------------"+num);
        if (num!=null&&num>0){
            //从缺勤表中获取学生的ID和课程的ID
            int courseId = absence.getCourseId();
            int studentId = absence.getStudentId();
            System.out.println("学生学号"+studentId+"课程ID"+courseId);
            //查询成绩表里面的缺勤字段
            Integer absence1 = absenceDao.queryAbsence(studentId, courseId);
            System.out.println("缺勤字段-----------------"+absence1);
            if (absence1!=null&& absence1>=0){
                ++absence1;
                int i = absenceDao.addAbsence(absence1, studentId, courseId);
                if (i>0){
                    System.out.println("成功---------------------------------");
                }else {
                    System.out.println("失败++++++++++++++++++++++++++++++++++++");
                }
            }
        }
        return num;
    }

    @Override
    public int deletedOne(int absenceId) {
        return absenceDao.deletedOne(absenceId);
    }
//模糊查询
    @Override
    public Set<AbsenceCourse> getByName(String studentName) {
        //获取当前的登录对象
        Session session = SecurityUtils.getSubject().getSession();
        User user = (User) session.getAttribute("user");
        //获取登录对象的id，用于查询当前院系对应的学生
        int userId = user.getUserId();
        String column="student_name";
        return absenceDao.queryByColumn(column,studentName,userId);
    }
}
